package fr.istic.linkedcv.client.presentation.impl;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootLayoutPanel;

import fr.istic.linkedcv.client.LinkedCv;
import fr.istic.linkedcv.client.presentation.Menu;
import fr.istic.linkedcv.client.presentation.Ui;
import fr.istic.linkedcv.client.presentation.views.View;
import fr.istic.linkedcv.client.presentation.views.impl.CVForm;
import fr.istic.linkedcv.client.presentation.views.impl.CVView;
import fr.istic.linkedcv.client.presentation.views.impl.Groupe;
import fr.istic.linkedcv.client.presentation.views.impl.GroupesForm;
import fr.istic.linkedcv.client.presentation.views.impl.InscriptionForm;
import fr.istic.linkedcv.client.presentation.views.impl.ListeAmis;
import fr.istic.linkedcv.client.presentation.views.impl.ListeGroupes;
import fr.istic.linkedcv.client.presentation.views.impl.ListeMessages;
import fr.istic.linkedcv.client.presentation.views.impl.ListePersonnesCli;
import fr.istic.linkedcv.client.presentation.views.impl.LoginForm;
import fr.istic.linkedcv.client.presentation.views.impl.Logout;
import fr.istic.linkedcv.client.presentation.views.impl.MessageForm;

public class UiImpl implements Ui {
	private DockLayoutPanel dock = new DockLayoutPanel(Unit.EM);

	HashMap<String, View> contentMap;

	private LinkedCv controle;

	private Map<String, String> properties = new HashMap<String, String>();
//	private final String initToken = History.getToken();
	
	public LinkedCv getControle() {
		return controle;
	}

	public UiImpl(LinkedCv linkedCv) {		
		this.controle = linkedCv;
		defineContentMap();
		build();
		RootLayoutPanel.get().add(dock);
		init();
		
		// Now that we've setup our listener, fire the initial history state.
		
	}

	@Override
	public void displayContent(String contentId) {
		dock.clear();
		View content = (contentMap.get(contentId) != null) ? contentMap.get(contentId) : contentMap.get("login");
		content.addProperties(properties);
		content.build();
		build();
		dock.add(content.getWidget());
		
		this.properties.clear();
	}

	public void init() {
		if (!controle.isLogged())
			addHistoryItem("login");
		
		History.addValueChangeHandler(this);
		fireCurrentHistoryState();
	}

	private void build() {
		dock.addNorth(new Menu(this), 2);
		
	}

	private void defineContentMap() {
		contentMap = new HashMap<String, View>();
		contentMap.put("inscription", new InscriptionForm(this));
		contentMap.put("liste_personnes", new ListePersonnesCli(this));
		contentMap.put("login", new LoginForm(this));
		contentMap.put("logout", new Logout(this));
		contentMap.put("create_cv", new CVForm(this));
		contentMap.put("create_groupe", new GroupesForm(this));
		ListeGroupes listMyGroupes = new ListeGroupes(this);
		listMyGroupes.addProperty("filter", "my");
		ListeGroupes listAllGroupes = new ListeGroupes(this);
		listAllGroupes.addProperty("filter", "all");
		contentMap.put("my_groupes", listMyGroupes);
		contentMap.put("liste_groupes", listAllGroupes);
		contentMap.put("groupe", new Groupe(this));
		contentMap.put("my_cv", new CVView(this));
		contentMap.put("voir_cv", new CVView(this));
		contentMap.put("liste_amis", new ListeAmis(this));
		contentMap.put("message_form", new MessageForm(this));
		contentMap.put("liste_messages", new ListeMessages(this));
	}

	@Override
	public void addProperty(String key,String property) {
		properties.put(key, property);		
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String[] anchor = event.getValue().split("-");
		String idContent = anchor[0];
		
		if(anchor.length>1){
			for(int i=1;i<anchor.length;i++){
				addProperty("parameter"+i, anchor[i]);
			}
		}
		displayContent(idContent);
	}

	@Override
	public void addHistoryItem(String item) {
		getControle().initPersonneCli();
		String initToken = History.getToken();
//		if (initToken.length() == 0) {
		      History.newItem(item);
//	    }
		
	}
	
	@Override
	public void fireCurrentHistoryState() {
		getControle().initPersonneCli();
		History.fireCurrentHistoryState();		
	}
	
	

}
